package com.jingogo.service.impl;

import com.jingogo.entity.Image;
import com.jingogo.mapper.ImageMapper;
import com.jingogo.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@Service
public class ImageServiceImpl implements ImageService {
    @Autowired
    private ImageMapper imageMapper;

    @Override
    public String uploadImage(MultipartFile file, Long userId) {
        // 实际应上传到对象存储并返回URL
        if (file == null || file.isEmpty()) throw new RuntimeException("文件不能为空");
        String url = "/mock/path/" + file.getOriginalFilename();
        Image image = new Image();
        image.setUrl(url);
        image.setFileName(file.getOriginalFilename());
        image.setUserId(userId);
        imageMapper.insert(image);
        return url;
    }

    @Override
    public void deleteImage(Long imageId) {
        imageMapper.deleteById(imageId);
    }

    @Override
    public void associateImage(Long imageId, String bizType, Long bizId) {
        Image image = imageMapper.selectById(imageId);
        if (image == null) throw new RuntimeException("图片不存在");
        image.setBizType(bizType);
        image.setBizId(bizId);
        imageMapper.update(image);
    }

    @Override
    public List<Image> getImagesByBiz(String bizType, Long bizId) {
        return imageMapper.selectByBiz(bizType, bizId);
    }
} 